import React, { Component } from 'react';

import { Row, Col, message,Input, Icon,Form, Cascader, DatePicker, Button, Tooltip, Popconfirm } from 'antd';
import moment from 'moment';
import DateRanger from '../../datecomponents/DateRanger'

class EditForm extends Component{
    constructor(props){
        super(props);

        console.log(this.props.data);

       

        const beginDate=moment().format('YYYY-MM-DD');
        const endDate='9999-12-31';
        this.state={
            value:null,
             inputerror :'none',
             dateRanger:{
                 start:this.props.data.dateRangeBeforeEdit.beginDate,
                 end:this.props.data.dateRangeBeforeEdit.endDate
             },
             nn:JSON.parse(JSON.stringify(this.props.data.nn)),
             adr:JSON.parse(JSON.stringify(this.props.data.adr)),
             sx:JSON.parse(JSON.stringify(this.props.data.sx))
        };
    }
    



     /*提交对象数据*/
     submitObjectInfo(e) {
        e.preventDefault();
        let self = this;
        let getstate = false;
        //console.log("objectID:", self.props.data);
        self.saveObject();
    }

    saveObject() {
        let self = this;
        let FieldListData = self.props.form.getFieldsValue();
        console.log(FieldListData);
        let objectId = FieldListData.objectID;
        let objectType = self.props.data.objectID.type;

        
        self.setState({nn:FieldListData.nn,adr:FieldListData.adr,sx:FieldListData.sx});
        if (self.props.closeEvent) {
            self.props.closeEvent();
        }
        // SaveObjectInfo({objectInfo: jsonData, objectForCreate: jsonNew, isNew: false}, (resData)=> {
        //     message.success(trans("common.savesuccess"), 2);
        //     jsonData.vId=resData.result.vId;
        //     if (self.props.onSaveSuccessed) {//此处应该刷新左侧的树的
        //         self.props.onSaveSuccessed(jsonData);
        //     }
        //     if (self.props.closeEvent) {
        //         self.props.closeEvent();
        //     }
        // });
    }


    /*取消编辑对象*/
    cancelEditObject() {
        let self = this;
        if (self.props.closeEvent) {
            self.props.closeEvent();
        }
        this.setState({
            nn: this.props.data.nn,
            adr: this.props.data.adr,
            sx:this.props.data.sx
        })
    }

    /*编辑对象*/
    onEdit() {
        if (this.props.onEdit) {
            this.props.onEdit();
        }
    }

     /*提交对象数据*/
     submitObjectInfo(e) {
        e.preventDefault();
        let self = this;
        let getstate = false;
        //console.log("objectID:", self.props.data);
        self.saveObject();
    }

    render(){
        let self = this;
       
        let name ="姓名";
        let addre ='地址';
        let sex = '性别';

        let cancel ='取消';
        let save = '保存';

        let Editing = self.props.editing === undefined ? true : self.props.editing;
        const {getFieldDecorator, getFieldError} = this.props.form;

        return(<Form>
                 <div className="c-tar">
                 {
                        Editing ? 
                        <Row>
                            <Col span={24} className="form-list">
                                <div className="org-object-time-div">
                                    <Col span={18} className="span-div">
                                    </Col> 
                                    <Col span={6} className="span-div">
                                        <div className="c-dib c-va-m">
                                        <DateRanger startValue={moment(this.state.dateRanger.start)}
                                                endValue={moment(this.state.dateRanger.end)}
                                                onStartChange={(value)=> {
                                                    let newState = this.state.dateRanger;
                                                    newState.start = value;
                                                    this.setState({dateRanger: newState})
                                                }}
                                                onEndChange={(value)=> {
                                                    let newState = this.state.dateRanger;
                                                    newState.end = value;
                                                    this.setState({dateRanger: newState})
                                                }}/>
                                        </div>
                                        <div className="c-dib c-va-m">
                                            <div className="c-dib">
                                            <Icon type="save" className="action-icon"
                                        onClick={(e)=> {
                                            self.submitObjectInfo(e)
                                        }}/>
                                            <Icon type="rollback" className="action-icon"
                                        onClick={()=> {
                                            self.cancelEditObject()
                                        }}/>
                                            </div>
                                        </div>
                                    </Col>
                                </div>
                            </Col>
                            </Row> :
                            <div className="c-tar org-object-time-div">
                                <div className="c-dib">{this.state.dateRanger && this.state.dateRanger.start ? moment(this.state.dateRanger.start).format('YYYY-MM-DD') : ""}~{this.state.dateRanger && this.state.dateRanger.end ? moment(this.state.dateRanger.end).format('YYYY-MM-DD') : ""}</div>
                                <Icon type="copy" title="复制" className="action-icon" onClick={()=>{this.onCopy()}}/>
                                <Icon type="copy" title="编辑" className="action-icon" onClick={()=>{this.onEdit()}}/>
                                {/* <div className="c-dib">
                                    <i className="icon iconfont icon-baocun action-icon" onClick={()=> {
                                        this.onEdit()
                                    }}/>
                                </div> */}
                            </div>
                    }
                 </div>

                  <Row>
                    <Col span={24} className="form-list">
                        <Col span={3} className="span-div">{name}</Col>
                        <Col span={5} className="div-view">
                            {
                                Editing ?
                                    getFieldDecorator('nn', {initialValue: this.state.nn})
                                    (<Input ></Input>) :
                                    <div>{this.state.nn}</div>
                            }
                        </Col>

                        <Col span={3} className="span-div">{addre}</Col>
                        <Col span={5} className="div-view">
                            {
                                Editing ?
                                    getFieldDecorator('adr', {initialValue: this.state.adr})
                                    (<Input ></Input>) :
                                    <div>{this.state.adr}</div>
                            }
                        </Col>

                        <Col span={3} className="span-div">{sex}</Col>
                        <Col span={5} className="div-view">
                            {
                                Editing ?
                                    getFieldDecorator('sx', {initialValue: this.state.sx})
                                    (<Input ></Input>) :
                                    <div>{this.state.sx}</div>
                            }
                        </Col>
                    </Col>
                </Row>
                <div>
                    {
                        Editing ?
                            <Row className="infoSetButton">
                                <Col span={6} offset={18} style={{textAlign: "right"}}>
                                    <Button className="btn-default c-mr-15"
                                            onClick={self.cancelEditObject.bind(self)}>{cancel}</Button>
                                    <Button className="btn-primary"
                                            onClick={self.submitObjectInfo.bind(self)}>{save}</Button>
                                </Col>
                            </Row> : null
                    }
                </div>
        </Form>);
    }

}

EditForm = Form.create()(EditForm);
export {
    EditForm as EditForm
}